<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="选择委托单位" prop="customerId">
        <el-select v-model="queryParams.customerId" placeholder="请选择委托单位" style="width: 240px">
          <el-option v-for="i in userListOptions" :key="i.userId" :label="i.nickName" :value="i.userId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :search="false">
      </right-toolbar>
    </el-row>
    <div style="overflow-x: auto; display: flex">
      <div v-for="city in cityOptions" :key="city.id" style="margin-right: 1rem">
        <div style="margin-bottom: 0.5rem">{{ city.projectName }}</div>
        <div>
          <el-card shadow="always" class="cardListSty" v-for="item in city.subPlanList" :key="item.id">
            <div class="divDetail">
              <!-- | dynamicText(cycleNoOptions) -->
              <span class="textSty" @click="handleDetail(item.id, item.status)">第{{
      item.cycleNo
    }}次维保任务，点击查看详情</span>
            </div>
            <div style="padding: 0 0 0.5rem 0.5rem">
              <span class="textSty">{{ item.projectName }}</span>
            </div>
            <div style="display: flex; align-items: center; padding-bottom: 10px">
              <div class="textIconSty">
                <i class="el-icon-time"></i>
                {{ item.planEndTime }} 截止
              </div>
              <el-button type="info" icon="el-icon-time" round size="mini" v-if="item.status == 1">{{ item.status |
      dynamicText(statusOptions) }}</el-button>
              <el-button type="primary" icon="el-icon-check" round size="mini" v-if="item.status == 2">{{ item.status |
      dynamicText(statusOptions) }}</el-button>
              <el-button type="primary" icon="el-icon-check" round size="mini" v-if="item.status == 3">{{ item.status |
      dynamicText(statusOptions) }}</el-button>
              <el-button type="success" icon="el-icon-check" round size="mini" v-if="item.status == 4">{{ item.status |
      dynamicText(statusOptions) }}</el-button>
              <el-button type="danger" icon="el-icon-remove-outline" round size="mini" v-if="item.status == 5">{{
      item.status | dynamicText(statusOptions) }}</el-button>
              <el-button type="success" icon="el-icon-check" round size="mini" v-if="item.status == 6">{{ item.status |
      dynamicText(statusOptions) }}</el-button>
            </div>
          </el-card>
        </div>
      </div>
    </div>
    <Detail v-if="detailVisible" ref="Detail" @refreshDetail="refreshDetail" />
    <editFloorList v-if="editFloorVisible" ref="Detail" @refresh="editFloorVisible = false" />
    <taskDetail v-if="taskDetailVisible" ref="taskDetail"></taskDetail>
  </div>
</template>

<script>
import {
  getUserList,
  getProjectPlanByCustomerId,
} from "@/api/plan-produce/task";

import Detail from "./detail.vue";
import editFloorList from "./editFloor.vue";
import taskDetail from "./task-detail.vue";
export default {
  name: "Building",
  components: { Detail, editFloorList, taskDetail },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 委托单位列表
      userList: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 建筑表格数据
      buildingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 详情页面
      detailVisible: false,
      // 编辑楼层
      editFloorVisible: false,
      taskDetailVisible: false,
      // 委托单位列表
      userListOptions: [],

      isIndeterminate: true,
      checkAll: false,
      checkInvert: false,
      cityOptions: [],
      checkedCities: [],
      checked: true,
      checkState: [],

      // 查询参数
      queryParams: {
        customerId: null,
      },

      cycleNoOptions: [
        { fullName: "一", id: 1 },
        { fullName: "二", id: 2 },
        { fullName: "三", id: 3 },
        { fullName: "四", id: 4 },
        { fullName: "五", id: 5 },
        { fullName: "六", id: 6 },
        { fullName: "七", id: 7 },
        { fullName: "八", id: 8 },
        { fullName: "九", id: 9 },
      ],
      statusOptions: [
        { fullName: "未预约", id: 1 },
        { fullName: "已预约", id: 2 },
        { fullName: "执行中", id: 3 },
        { fullName: "已完成", id: 4 },
        { fullName: "未完成", id: 5 },
        { fullName: "已完结", id: 6 },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serviceOrgId: [
          {
            required: true,
            message: "消防技术服务机构id不能为空",
            trigger: "blur",
          },
        ],
        customerId: [
          { required: true, message: "委托单位不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },

  methods: {
    // 全选
    handleCheckAllChange(val) {
      this.checkedCities = val ? this.cityOptions : [];
      this.getInEl(this.cityOptions, this.checkedCities);
    },
    // 反选
    handleInvertCheckChange(val) {
      let cities = this.cityOptions;
      let checkedCities = this.checkedCities;
      if (checkedCities.length === 0) {
        checkedCities = val ? cities : [];
      } else if (checkedCities.length === cities.length) {
        this.checkedCities = [];
        this.checkAll = false;
      } else {
        let list = cities.concat(checkedCities).filter((v, i, array) => {
          return array.indexOf(v) === array.lastIndexOf(v);
        });
        this.checkedCities = list;
      }
      this.getInEl(this.cityOptions, this.checkedCities);
    },
    // 选择
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cityOptions.length;
      this.getInEl(this.cityOptions, value);
    },
    // 选择公共代码
    getInEl(a, b) {
      this.checkState = [];
      for (let i = 0; i < a.length; i++) {
        this.checkState.push(b.includes(a[i]));
      }
    },
    getList() {
      this.loading = true;
      // 获取用户列表
      getUserList("02").then((response) => {
        this.userListOptions = response.data;
        this.total = response.total;
        this.loading = false;
      });
      getProjectPlanByCustomerId(this.queryParams.customerId || "-1").then(
        (res) => {
          this.cityOptions = res.data;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serviceOrgId: null,
        customerId: null,
        name: null,
        address: null,
        pointPositionDesc: null,
        abovegroundNumber: null,
        undergroundNumber: null,
        longitude: null,
        latitude: null,
        invalid: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 跳转详情
    handleDetail(id, ids) {
      if (ids != 1) {
        this.detailVisible = true;
        this.$nextTick(() => {
          this.$refs.Detail.init(id);
        });
      } else {
        this.$modal.msgWarning('未预约无法查看详情');
      }
    },
    refreshDetail(list) {
      this.detailVisible = false;
      setTimeout(() => {
        this.taskDetailVisible = true;
        this.$nextTick(() => {
          this.$refs.taskDetail.init(list);
        });
      }, 0);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBuilding(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuilding(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped rel="stylesheet/scss">
.mapSty {
  width: 100%;
  height: 450px;
  position: relative;
}

#allMap {
  width: 100%;
  height: 450px;
}

.divDetail {
  padding: 0.5rem 0 0.5rem 0.5rem;
  cursor: pointer;
  /* 设置鼠标样式为手型 */
}

.textSty {
  font-size: 14px;
  color: rgb(102, 102, 102);
}

.cardListSty {
  height: 6.5rem;
  width: 16.5rem;
  margin-bottom: 1rem;
}

.textIconSty {
  margin-left: 10px;
  font-size: 12px;
  color: rgb(102, 102, 102);
  margin-right: 0.3rem;
  // flex: 1;

  .listIcon {
    padding: 0 5px;
  }
}

.text-with-line {
  text-decoration: line-through;
}

::v-deep .el-card__body {
  padding: 0;
}
</style>
